<?php
/**
 * Created by PhpStorm.
 * User: kaycn
 * Date: 18-9-12
 * Time: 下午11:36
 */

namespace Pay;

use Facebook\WebDriver\WebDriverBy;
use Facebook\WebDriver\WebDriverExpectedCondition;
use Facebook\WebDriver\Remote\DesiredCapabilities;
use Facebook\WebDriver\Remote\RemoteWebDriver;
use Pay\Screen;

class Jpay
{
    public $driver;

    public $code;

    public function __construct()
    {
        $this->__init();
    }

    public function __init()
    {

        // start Firefox with 5 second timeout
        $host = 'http://localhost:4444/wd/hub'; // this is the default
        $capabilities = DesiredCapabilities::chrome();
        $driver = RemoteWebDriver::create($host, $capabilities, 5000);
        $driver->get('https://www.alipay.com/');

        $element = $driver->findElement(WebDriverBy::cssSelector('.title>.personal'));

        $driver->executeScript("arguments[0].click();", [$element]);


// 等待新的页面加载完成....
        $driver->wait(2)->until(
            WebDriverExpectedCondition::visibilityOfElementLocated(
                WebDriverBy::id('J_onepage')

            )
        );
        $element = $driver->findElement(WebDriverBy::cssSelector('.button-login'));

        $driver->executeScript("arguments[0].click();", [$element]);

        $driver->wait(2)->until(
            WebDriverExpectedCondition::visibilityOfElementLocated(
                WebDriverBy::id("J_loginIframe")

            )
        );

//$element = $driver->findElement(WebDriverBy::cssSelector('#J_loginbox>.qrcode-target-hide'));
        $driver_old = $driver;
        $driver->switchTo()->frame("J_loginIframe");
        $str = $driver->getPageSource();
        $element = $driver->findElement(WebDriverBy::cssSelector('.qrcode-target-hide'));

        $driver->executeScript("arguments[0].click();", [$element]);



        $this->driver = $driver;

    }

    public function sendcontent(){
        sleep(5);

        $this->driver->findElement(WebDriverBy::id('J-input-user'))->sendKeys("346312616@qq.com");

        sleep(5);

        $this->driver->findElement(WebDriverBy::id('password_rsainput'))->sendKeys("19960521Aa");

        sleep(5);

        $this ->sendcode();

        sleep(2);
//点击
        $this->driver->findElement(WebDriverBy::id('J-login-btn'))->click();


        sleep(5);
        if ($this->driver->getTitle() == "支付宝 知托付！") {
            $a = [1, 9, 9, 6, 0, 5, 2, 1, "A", "a"];
            foreach ($a as $key) {

                $this->driver->findElement(WebDriverBy::id('password_rsainput'))->sendKeys($key);

                echo $key;

                sleep(1);
            }
           $this ->sendcode();


            $this->driver->findElement(WebDriverBy::id('J-login-btn'))->click();
        }

        return $this;



    }
    public static $length;

    public function getcount($i=0,$menth){


        $this->driver->get("https://mbillexprod.alipay.com/enterprise/tradeListQuery.htm");

        if($this->driver->getTitle()=="登录 - 支付宝"){
            $this -> __init();
        }

        $Js = Js::getJs($this->driver);

         $Js->$menth();

        sleep(1);

        return $Js->get($i);


    }

    public function getcount_all($menth){


        $this->driver->get("https://mbillexprod.alipay.com/enterprise/tradeListQuery.htm");

        if($this->driver->getTitle()=="登录 - 支付宝"){
            $this -> __init();
        }

        $Js = Js::getJs($this->driver);

        $Js->$menth();

        sleep(2);

        return $Js->getall();


    }

    public function sendcode(){
        if(!$this->checkimg()){

            (new \Pay\Screen($this->driver))->frame();

            $app_id = 305603;
            $app_key = "BDK07osOtoHrWZ2EBI5+7s7I61wcWFnX";
//pd账号id&key
            $usr_id = 105603;
            $usr_key = "335YeT6HRUIyDMaJaf2C6/BMYwcHaD8V";

            $a = json_decode((new img($app_id, $app_key, $usr_id, $usr_key))->deel("vcode.png")["RspData"], true);
            $a = $a["result"];
            $this->code =  $a;
            echo $a;
            //输入J-input-checkcode
            $this->driver->findElement(WebDriverBy::id('J-input-checkcode'))->sendKeys($a);
        }
    }

    public function checkimg()
    {
        $driver = $this->driver;

        $hid = $driver->findElement(WebDriverBy::id('J-checkcode'))->getAttribute("class");
        $check = preg_match('/fn-hide/', $hid);

        return $check;





    }

}